﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProgNetDxComponents.ExtenderProviders.Options
{
    public static class OptionCache
    {
        public static event EventHandler<OptionChangedEventArgs> OptionChanged
        {
            add { OptionGrabber.OptionChanged += value; }
            remove { OptionGrabber.OptionChanged -= value; }
        }

        private static bool OptionsGrabbed = false;
        public static OptionGrabber Grabber
        {
            get;
            set;
        }

        private static OptionCollection AllOptions = new OptionCollection();
        public static void RegisterOption(Option o)
        {
            AllOptions.Add(o);
        }
        public static void RegisterOptions(OptionCollection options)
        {
            foreach (Option o in options)
                RegisterOption(o);
        }

        public static object GetOptionValue(string option)
        {
            RefreshIfNeeded();
            object res = null;
            Option o = AllOptions[option];
            if (Grabber.HasOption(option))
                res = Grabber.GetOptionValue(option);
            else
                res = o.DefaultValue;

            if (res != null)
                if (o.Encrypt && res.IsNotNull())
                    res = res.AsString().DecryptDes();
            if (res != null)
                try
                {
                    res = Convert.ChangeType(res, o.OptionType);
                }
                catch { }
            return res;
        }
        public static bool GetOptionBoolValue(string option)
        {
            return GetOptionValue(option).AsBool();
        }
        public static string GetOptionStringValue(string option)
        {
            return GetOptionValue(option).AsString();
        }
        public static void SetOptionValue(string option, object value)
        {
            RefreshIfNeeded();
            if (value.IsNotNull())
            {
                Option o = AllOptions[option];
                if (o.Encrypt && value.IsNotNull())
                    value = value.AsString().EncryptDes();
            }
            Grabber.SetOptionValue(option, value);
        }

        public static void SetOptionValueDirect(string option, object value)
        {
            RefreshIfNeeded();
            if (value.IsNotNull())
            {
                Option o = AllOptions[option];
                if (o.Encrypt)
                    value = value.AsString().EncryptDes();
            }
            Grabber.SetOptionValueDirect(option, value);
        }

        public static bool SaveAllOptions()
        {
            RefreshIfNeeded();
            return Grabber.SaveAllOptions();
        }
        private static void RefreshIfNeeded()
        {
            if (!OptionsGrabbed)
            {
                OptionsGrabbed = true;
                Grabber.GetAllOptions();
            }
        }
    }
}
